package com.yz.lzq.liwushuo.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.bean.CategoryBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by AdminJia on 2016/12/9.
 */

public class CategoryGongLHeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<CategoryBean.CategoryGongLHeaderBean.DataBean.ColumnsBean> mList = null;
    private Context mContext = null;
    private static final int NOMAL = 0;
    private static final int ALL = 1;
    private View view = null;


    public CategoryGongLHeaderAdapter(List<CategoryBean.CategoryGongLHeaderBean.DataBean.ColumnsBean> mList,
                                      Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < mList.size()) {
            return 0;
        }
//        if (position < mList.size() && mList.get(position) instanceof CategoryGongLHeaderBean
//                .DataBean.ColumnsBean)

        else return 1;

    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i("TAG", "-------------RecyclerView.ViewHolder: ");
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(mContext).inflate(R.layout
                        .item_category_gongl_header_gridlayout, parent, false);
                return new ViewHolder1(view);

            case 1:
                view = LayoutInflater.from(mContext).inflate(R.layout
                        .item_category_gongl_header_gridlayout_all, parent, false);
                return new ViewHolderAll(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder1) {
            ((ViewHolder1) holder).simpleDraweeViewCategoryGongLHeader.setImageURI(Uri.parse
                    (mList.get(position).getBanner_webp_url() + ""));
            ((ViewHolder1) holder).textViewCategoryGongLHeaderName.setText(mList.get(position)
                    .getAuthor());
            ((ViewHolder1) holder).textViewCategoryGongLHeaderTitle.setText(mList.get(position)
                    .getTitle() + "woshi");
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (getItemViewType(position)) {
                    case 0:
                        Toast.makeText(mContext, mList.get(position).getAuthor(), Toast
                                .LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(mContext, ((ViewHolderAll) holder)
                                .textViewCategoryGongLHeaderAll.getText(), Toast.LENGTH_SHORT)
                                .show();
                }
            }
        });

    }

//
//    @Override
//    public int getCount() {
//        return mList.size() + 1;
//    }
//
//    @Override
//    public Object getItem(int i) {
//        return mList.get(i);
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return i;
//    }
//
//    @Override
//    public View getView(final int i, View view, ViewGroup viewGroup) {
//
//        View contentView = null;
//
//        if (i < mList.size()) {
//            if (view == null) {
//                contentView = LayoutInflater.from(mContext).inflate(R.layout
//                        .item_category_gongl_header_gridlayout, viewGroup, false);
//                view = contentView;
//                viewHolder = new ViewHolder(view);
//                view.setTag(viewHolder);
//            } else {
//                viewHolder = (ViewHolder) view.getTag();
//            }
//            viewHolder.simpleDraweeViewCategoryGongLHeader.setImageURI(Uri.parse(mList.get(i)
//                    .getBanner_webp_url() + ""));
//
//
//        } else {
//            if (view == null) {
//                contentView = LayoutInflater.from(mContext).inflate(R.layout
//                        .item_category_gongl_header_gridlayout_all, viewGroup, false);
//                view = contentView;
//                viewHolderAll = new ViewHolderAll(contentView);
//                view.setTag(contentView);
//            } else {
//                viewHolderAll = (ViewHolderAll) view.getTag();
//            }
//
//        }
//        view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent();
//                if (i < mList.size()) {
//                    Toast.makeText(mContext, "点击了第" + i + "项", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//        return view;
//    }
//

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.simpleDraweeView_category_gongL_header)
        SimpleDraweeView simpleDraweeViewCategoryGongLHeader;
        @Bind(R.id.textView_category_gongL_header_name)
        TextView textViewCategoryGongLHeaderName;
        @Bind(R.id.textView_category_gongL_header_title)
        TextView textViewCategoryGongLHeaderTitle;

        public ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    class ViewHolderAll extends RecyclerView.ViewHolder {
        @Bind(R.id.textView_category_gongL_header_all)
        TextView textViewCategoryGongLHeaderAll;

        ViewHolderAll(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
